package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersDTO;
import com.sky.dto.OrdersHistoryDto;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderStatisticsVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

public interface OrdersMapper {
    /**
     * 插入订单数据
     * @param orders
     */
    void insert(Orders orders);

    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 根据订单id和用户ID查询订单
     * @param orderId
     * @param userId
     * @return
     */
    @Select("select * from orders where id = #{orderId} and user_id= #{userId}")
    Orders getByOrderIdAndUserId(Long orderId, Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);
    /**
     * 查询历史订单详情
     * @param status
     * @return
     */
    Page<OrdersHistoryDto>  historyOrders(OrdersPageQueryDTO status);

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    Orders getById(Long id);

    /**
     * 管理端订单搜索
     * @param dto
     * @return
     */
    Page<Orders> conditionSearch(OrdersPageQueryDTO dto);

    /**
     * 各个状态的订单数量统计
     * @return 订单统计结果
     */
    OrderStatisticsVO statistics();

    /**
     * 根据id删除订单数据
     * @param id
     */
    @Delete("delete from orders where id = #{id}")
    void deleteById(Long id);
    @Select("select * from orders where status = #{status} and order_time <= #{orderTime}")
    List<Orders> selectByStatusAndOrderTime(Integer status, LocalDateTime orderTime);

    /**
     * 查询当当天的营业额
     * @return
     */
    @Select("select sum(amount) from orders where status = #{status} and order_time between #{beginTime} and #{endTime}")
    Double sumByMap(Map map);
}
